package Behaviour;

import jade.core.behaviours.OneShotBehaviour;

import java.awt.Point;
import java.util.List;

import Model.Car;

import map.Map;


public class ManageWaitList extends OneShotBehaviour {

	private static final long serialVersionUID = 1L;
	private Map map;
	private int capacity;
	private List<Point> entries;
	
	public ManageWaitList(Map map){
		this.map = map;
		this.capacity = map.getCapacity();
		this.entries = map.getKeyEntryList();
	}
	

	@Override
	public void action() {
		// CONTROLLO CHE LA PRIMA AUTO IN WAIT NELLE LISTE DI ATTESA POSSA ENTRARE
		Car toAdd;
		if(this.map.getCars().size() < this.capacity){
			for (Point p : entries) {
				if(this.map.getCars().size() < this.capacity){
					toAdd = this.map.getFirstCarFromWaitList(p);
					if(toAdd != null){
						if(!this.map.getMap()[toAdd.getSource().x][toAdd.getSource().y].isOccupied()){
							this.map.addCar(toAdd);
							this.map.getMap()[toAdd.getSource().x][toAdd.getSource().y].setOccupied(true);
							this.map.removeFirstCarFromWaitLists(p);
						}
					}
				}
			}
		}
	}



}
